import java.io.*;
import java.net.*;
import java.util.*;

public class Main {


    public static String serverPassword = "zhumadianYiJunNMSL";
    public static String hwidlist = "a27276992737f9f4877c543f4cc7a60ba0bbd1d2fdbb18e4ceb71c257179fd5";

//    public static List<String> list;
//
//    public static void setList(List<String> list) {
//        Main.list = list;
//        list.add("a27276992737f9f4877c543f4cc7a60ba0bbd1d2fdbb18e4ceb71c257179fd5"); // Oxu_uxO | _NEVERL0SE_
//    }

    private static final Set<PrintWriter> clients = new HashSet<>();


    public static void main(String[] args) {
        try (ServerSocket serverSocket = new ServerSocket(19730)) {
            System.out.println("Server started...");
            while (true) {
                new ClientHandler(serverSocket.accept()).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static class ClientHandler extends Thread {
        private Socket socket;
        private PrintWriter out;
        private BufferedReader in;

        public ClientHandler(Socket socket) {
            this.socket = socket;
        }

        public void run() {
            try {
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                out = new PrintWriter(socket.getOutputStream(), true);
                synchronized (clients) {
                    clients.add(out);
                }
                String message;
                while ((message = in.readLine()) != null) {
                    System.out.println("Received: " + message);

                    String[] parts = message.split(" ");
                    String tz = parts[0];
                    String playername;

                    System.out.println(parts.length);


                    if (tz.equalsIgnoreCase("hwidauth")){
                        playername = parts[2];
                        if (hwidlist.contains(parts[1])){
                            broadcast("10 1 1 1 1 1 " + playername + " 1 1 1 1");
                        }
                    } else if (parts.length == 11){

                    } else if (hwidlist.contains(parts[11])){
                        broadcast(message);
                    }



                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                synchronized (clients) {
                    clients.remove(out);
                }
            }
        }

        private void broadcast(String message) {
            synchronized (clients) {
                for (PrintWriter client : clients) {
                    client.println(message);
                }
            }
        }
    }

    public static String toUnicode(String input) {
        StringBuilder unicode = new StringBuilder();
        for (char c : input.toCharArray()) {
            unicode.append("\\u").append(String.format("%04x", (int) c));
        }
        return unicode.toString();
    }

}